import routes, { routeItem } from '@/router';
/*
 * 数组对象去重
 * */
export const getSingArray = (list: any[], key: string) => {
  const obj = {};
  return list.reduce((cur, next) => {
    // @ts-ignore
    // eslint-disable-next-line @typescript-eslint/no-unused-expressions
    obj[next[key]] ? '' : (obj[next[key]] = cur.push(next));
    return cur;
  }, []);
};

/*
 * 判断当前路由是否有权限
 * */
export const getAuthority = (list: routeItem[], value: string) => {
  const paths: string[] = [];
  list.forEach((item) => {
    if (item.children && item.children.length) {
      item.children.forEach((t) => {
        paths.push(t.path);
      });
    } else {
      paths.push(item.path);
    }
  });
  return paths.includes(value);
};

/*
 * 获取组件
 * */

export const getComponent = (pathname: string) => {
  const route = getRoute().filter((t) => t.path == pathname);
  const { component } = route[0];
  return component;
};

/*
 * 扁平化路由
 * */
export const getRoute = () => {
  const arr: routeItem[] = [];
  // @ts-ignore
  routes[1]?.children.forEach((item) => {
    if (item.children && item.children.length) {
      item.children.forEach((t) => {
        arr.push(t);
      });
    } else {
      arr.push(item);
    }
  });
  return arr;
};
